
package com.pekall.music;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.pekall.music.adapter.MusicListAdapter;
import com.pekall.music.model.Music;
import com.pekall.music.model.MusicPositionInfo;
import com.pekall.music.service.IPlayService;
import com.pekall.music.service.MusicService;
import com.pekall.music.util.MusicOperate;
import com.pekall.music.util.Util;
import com.pekall.music.view.ViewDragableSpace;
import com.pekall.music.view.ViewLrc;

public class HomeActivity extends Activity implements OnClickListener {
    /**
     * service绑定
     */
    private ServiceConnection serviceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            countService = (IPlayService) service;
        }

        public void onServiceDisconnected(ComponentName name) {
            countService = null;
        }
    };

    /**
     * 实例musicoperate
     */
    private MusicOperate mMusicOperate;

    /**
     * 歌曲list
     */
    private List<Music> mListMusic;

    /**
     * 滑动ViewGroup
     */
    private ViewDragableSpace mViewDragableSpace;

    /**
     * 显示歌曲列表
     */
    private ListView mListView;

    /**
     * 歌词秀
     */
    private ViewLrc mViewLrc;

    /**
     * Layout填充
     */
    private LayoutInflater mLayoutInflater;

    /**
     * 滑动group填充view
     */
    private View mLayoutView;

    /**
     * 当前选中MusicId
     */
    private int mCurrentMusicId;

    /**
     * 当前选中music
     */
    private Music mCurrentMusic;

    /**
     * service传回的播放信息
     */
    private MusicPositionInfo mMusicPositionInfo;

    /**
     * service name
     */
    private ComponentName component;

    private boolean threadDisable;

    private IPlayService countService;

    private ImageButton mBtnPrev;

    private ImageButton mBtnPlay;

    private ImageButton mBtnNext;

    private ImageView mImgPlay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.homeactivity);
        mMusicOperate = new MusicOperate();
        mMusicPositionInfo = new MusicPositionInfo();
        mViewDragableSpace = (ViewDragableSpace) findViewById(R.id.viewdragablespace);
        mLayoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        /* listlayout */
        mLayoutView = findViewById(R.id.listview);
        mListView = (ListView) mLayoutView.findViewById(R.id.playList);
        mListMusic = mMusicOperate.getAllMusic(mLayoutInflater.getContext());
        mListView.setAdapter(new MusicListAdapter(this.mLayoutInflater.getContext(), mListMusic));
        /* lrclayout */
        mLayoutView = findViewById(R.id.lrcview);
        mViewLrc = (ViewLrc) mLayoutView.findViewById(R.id.lyrics);
        /* playlayout */
        mLayoutView = findViewById(R.id.playview);
       
        /* 播放控制 */
        mBtnPrev = (ImageButton) findViewById(R.id.btnPrev);
        mBtnPlay = (ImageButton) findViewById(R.id.btnPlay);
        mBtnNext = (ImageButton) findViewById(R.id.btnNext);
        mImgPlay = (ImageView) findViewById(R.id.imgPlay);
        mBtnPrev.setOnClickListener(this);
        mBtnPlay.setOnClickListener(this);
        mBtnNext.setOnClickListener(this);
        mImgPlay.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();

        mViewDragableSpace.setScreen(0);
        mCurrentMusicId = 0;
        mCurrentMusic = mListMusic.get(mCurrentMusicId);

        component = new ComponentName(this, MusicService.class);

        this.bindService(new Intent("com.pekall.music.service.MusicService"),
                this.serviceConnection, BIND_AUTO_CREATE);
        new Thread(new Runnable() {

            public void run() {
                while (!threadDisable) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                    }
                    if (countService != null) {
                        flashView();
                    }
                }
            }
        }).start();

    }

    private void flashView() {
        mMusicPositionInfo = countService.getMusicPlayingInfo();
        mCurrentMusic = mListMusic.get(mMusicPositionInfo.id);
        /* --------刷新前台界面----------- */
        mViewLrc.setlrcContent("/sdcard/" + "music.lrc");
        mViewLrc.setlrcPosition(mMusicPositionInfo.position);
        /* ----------------------------- */
    }

    public void onClick(View v) {
        if (v == mBtnPrev) {
            Intent mIntent = new Intent(Util.PREVIOUS_ACTION);
            mIntent.setComponent(component);
            startService(mIntent);
            mViewLrc.Star();
        } else if (v == mBtnPlay) {
            Intent mIntent = new Intent(Util.PLAY_ACTION);
            mIntent.setComponent(component);
            startService(mIntent);
            mViewLrc.Star();
        } else if (v == mBtnNext) {
            Intent mIntent = new Intent(Util.NEXT_ACTION);
            mIntent.setComponent(component);
            startService(mIntent);
            mViewLrc.Star();
        }
    }

    protected void onDestroy() {
        /* 解除绑定 */
        this.unbindService(serviceConnection);
        this.threadDisable = true;
        super.onDestroy();
    }
}
